
local weights = {}
local odds = {80,18,2}
for k,v in pairs(load_excel('物品数据.xlsx','黑市')) do
    local name = v.name
    local lv = v['稀有度']
    local item = ac.lni('item',name)
    
    --item.stockMax = v['购买次数']
    item.food = v['杀敌数需求']
    item.item_class = '黑市'
    table.insert(weights,{name,odds[lv]})
end


--孤岛商人
local shop_list = {}
ac.game:event '游戏-显示商店'(function()
    for _,unit in ipairs(shop_list) do
        unit:remove_restriction '隐藏'
    end
end)

ac.game:event '游戏-隐藏商店'(function()
    for _,unit in ipairs(shop_list) do
        unit:add_restriction '隐藏'
    end
end)

ac.game:event '游戏-删除商店'(function()
    for _,unit in ipairs(shop_list) do
        unit:remove()
    end
end)

ac.game:event '玩家-刷新黑市'(function(_,player)
    local unit = player:get_data('孤岛商人')
    local weight = table.copy(weights)
    local shops = unit.shops or {}
    for _,skill in ipairs(shops) do
        skill:remove()
    end
    shops = {}
    unit.shops = shops
    
    for a=1,8 do
        local name,index = table.get_by_weight(weight)
        local skill = unit:add_sell(name)
        skill.supply = player
        shops[a] = skill
        table.remove(weight,index)
    end
    if time and unit.update_timer then
        local time = unit.update_timer:get_remaining()
        local func = unit.update_timer.on_timer
        unit.update_timer:remove()
        unit.update_timer = ac.loop(time,func)
    end
end)

ac.game:event '玩家-击败挑战怪'(function(_,player,count)
    if count==3 then
        local unit = player:get_data('随机事件')
        unit:remove_buff '未解锁'
    end
    if count==5  then
        local unit = player:get_data('炮台')
        unit:remove_buff '未解锁'
    end

    if count==8 then
        local unit = player:get_data('孤岛商人')
        unit:remove_buff '未解锁'
    end
end)

ac.wait(100,function()
    ac.each_player(function(player)
        local unit1 = player:create_unit('炮台',player.shop2,270)
        local unit2 = player:create_unit('孤岛商人',player.shop1,270)
        local unit3 = player:create_unit('随机事件',player.shop3,270)
        unit1:add_ability('A002')
        unit1:add_buff '未解锁'{ui = '挑战LV5解锁'}
        unit2:add_buff '未解锁'{ui = '挑战LV8解锁'}
        unit3:add_buff '未解锁'{ui = '挑战LV3解锁'}
        player:set_data('炮台',unit1)
        player:set_data('孤岛商人',unit2)
        player:set_data('随机事件',unit3)
        table.insert(shop_list,unit1)
        table.insert(shop_list,unit2)
        table.insert(shop_list,unit3)
    end)
    local rect = ac.rect.j_rect('base')
    local point = rect:get_point()
    --基地
    local base = ac.player[5]:create_unit('基地',point)
    ac.base_unit = base
    base:set('生命上限',100)
    base:set('生命恢复',1)
    unit_add_life2(base,1.5)
    table.insert(shop_list,base)

    base:event '单位-即将受到伤害'(function(_,_,target,data)
        if data:is_attack() then
            data.current_damage = 1
            if target:is_type('精英') then
                data.current_damage = 3
            end
            if target:is_type('Boss') then
                data.current_damage = 10
            end
            ac.player.self:send_msg('|cffff0000基地正受到攻击!!!!|r')
        else
            data.current_damage = 0
        end
    end)
    base:event '单位-死亡'(function(_,_,target,data)
        ac.enemy:clear()
        ac.game:notify('游戏-游戏失败',ac.enemy)
    end)

end)


local buff = ac.buff['未解锁']

function buff:on_add()
    local unit = self.owner
    local panel = shop_add_name(unit,self.ui)
    local player = unit.owner
    local name = unit.name
    unit:set_color(0x88000000)
    self:gc(function()
        unit:set_color(0xffffffff)
        panel:remove()

        player:set_camera(unit:get_point(),1)
        if name=='随机事件' then
            player:notify('玩家-刷新随机事件',player)
            shop_add_panel(unit)
            unit:set('生命上限',300)
        end

        if name=='炮台' then
            unit:add_skill('升级范围','英雄')
            unit:add_skill('升级威力','英雄')
            unit:add_skill('升级效率','英雄')
            unit:add_skill('升级速度','英雄')
            unit:add_skill('打鱼','英雄')
            unit:add_skill('自动打鱼','英雄')
        end
        if name=='孤岛商人' then
            shop_add_panel(unit)
            unit:set('生命上限',120)
            player:set_camera(unit:get_point(),1)
            unit.update_timer = unit:loop(120 * 1000,function()
                player:notify('黑市刷新',player,unit)
                player:notify('玩家-刷新黑市',player)
            end)
            unit:loop(1 * 1000,function()
                local time = unit.update_timer:get_remaining()/1000
                unit:set('生命',unit:get('生命上限') - time)
                unit.ui_title = ('刷新：%0.f'):format(time)
            end)
            unit.update_timer:on_timer()
        end
        
    end)
end
    